package com.javaclimb.music.service.realm;

import com.javaclimb.music.dao.AdminMapper;
import com.javaclimb.music.dao.ConsumerMapper;
import com.javaclimb.music.domain.Consumer;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class ShiroUserRealm extends SimpleCredentialsMatcher {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private ConsumerMapper consumerMapper;


 /*   *//**
     * 密码比较的规则
     * token:用户在界面输入的用户名和密码
     * info: 从数据库中得到的加密数据
     *//*
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        // 获取用户输入的密码，并加密
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        String md5Pwd = Encrypt.md5(new String(upToken.getPassword()), upToken.getUsername());

        // 获取数据库中的加密密码
        String pwd = (String) info.getCredentials();

        // 返回比较结果
        return this.equals(md5Pwd, pwd);
    }

    public void setConsumerService(String consumerService) {
    }

    public void setCredentialsMatcher(String credentialsMatcher) {
    }*/

 /*   @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        Admin user = (Admin) principalCollection.getPrimaryPrincipal();
//        List<Integer> roleIds=consumerMapper.
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        UsernamePasswordToken upToken = (UsernamePasswordToken) authenticationToken;
//        String username = upToken.getUsername();
//        Consumer user = consumerMapper.getByUsername(username);
//        if (username==null)
//            throw new UnknownAccountException();
//        if(user.getValid()==0) throw new LockedAccountException();
//        ByteSource credentialSalt = ByteSource.Util.bytes(user.getSalt());
//        SimpleAuthenticationInfo info =
//                new SimpleAuthenticationInfo(user, user.getPassword(), credentialSalt, getName());
//        return info;
    }*/

}
